/*************************************************
Author: will
Date: 9/11/2012
Description:
Notes: 
*************************************************/

#ifndef __CUserTask_h__
#define __CUserTask_h__

#include "CTArray.h"
#include "CValidTask.h"
#include "CTaskStatus.h"
#include "CTaskWaitForOpen.h"
#include "CTaskTargetTrigger.h"
#include "../item/CDropItem.h"
#include "CTaskConfig.h"
#include "../cmdfront/cmdtask.h"

class CTaskMgr;
class CUserTask
{
    friend class CTaskMgr;
public:
	CUserTask() : _uiGID(0), _uiMarketTaskID(0)
    {
	}

	virtual ~CUserTask() 
    {
	}

    bool IsFull() const 
    {
        return _astValidTasks.Size() == MAX_VALID_TASK;
    }

    void Clean();

    int Init(CUserTaskData& stUserTaskData);
    int GetData(CUserTaskData& stUserTaskData);

    int AddOpenTaskData(CTaskID& stTaskID);
    int AddValidTaskData(CValidTask& stValidTask);
    int DelValidTaskData(CTaskID& stTaskID);
    int AddWaitForOpenData(CTaskWaitForOpen& stWaitForOpen);

    int AddValidTaskTrigger(unsigned char ucTaskIndex, CValidTask& stValidTask);
    int AddWaitForOpenTrigger(unsigned char ucTaskIndex, CTaskWaitForOpen& stWaitForOpen);
    
    int AddTrigger(unsigned char ucCondType, CTaskTargetTrigger& stTrigger);
    
    int AddLevelUpTrigger(CTaskTargetTrigger& stTrigger);
    int AddBossConquerTrigger(CTaskTargetTrigger& stTrigger);
    int AddTaskFinishTrigger(CTaskTargetTrigger& stTrigger);
    int AddPetLevelUpTrigger(CTaskTargetTrigger& stTrigger);
    int AddKillEnemyTrigger(CTaskTargetTrigger& stTrigger);
    int AddDoSomeOptTrigger(CTaskTargetTrigger& stTrigger);
    int AddUseItemTrigger(CTaskTargetTrigger& stTrigger);
    int AddGetItemTrigger(CTaskTargetTrigger& stTrigger);
    int AddFinishInstanceTrigger(CTaskTargetTrigger& stTrigger);

    //
    int UpdateValidTask(CTaskTargetTrigger& stTaskTargetTrigger, CTaskCond& stCond);
    int UpdateWaitForOpenTask(CTaskTargetTrigger& stTaskTargetTrigger, CTaskCond& stCond);

    //检查等待激活的任务，如果满足激活条件则激活
    int CheckWaitForOpenTask();

    //检查进行中的任务
    int CheckValidTask();

    int CleanTrigger(unsigned char ucCondType, CTaskID& stTaskID);
    int CleanTrigger(CTLib::CTArray<CTaskTargetTrigger, MAX_TRIGGER_PER_TYPE>& astTriggers, CTaskID& stTaskID);

    bool IsCanAcceptTask(CTaskID& stTaskID);

    int CleanOpenTask(CTaskID& stTaskID);

    int CommitTask(CTaskID& stTaskID);

    bool IsValidTask(CTaskID& stTaskID);

    //有的任务在任务状态变化时需要被自动接取
    bool HasTaskNeedToAccept() const {return _astTaskNeedAccept.Size() > 0;}
    CTLib::CTArray<CTaskID, MAX_NEED_TO_ACCEPT>& GetTaskNeedAcceptList() {return _astTaskNeedAccept;}
    void CleanTaskNeedAcceptList() {_astTaskNeedAccept.Clear();}

    bool IsTaskFinished(CTaskID& stTaskID);

    void DumpTaskInfo();
    void DumpTriggerInfo();
    void SetUserGid(unsigned int uiGid);
    unsigned int GetUserGid() const;
    int GetTaskAwardItems(CTaskConfigData& curTaskCfgData, CDropItem& stDropItem);

    //GM set task finished
    int GMSetTaskFinished(CTaskID & iTaskId);

    //检查已经完成的任务
    void CheckTaskHasFinished(CTaskID stTaskID);

    void SetMarketTaskID (unsigned int uiMarketTaskID);

    unsigned int GetMarketTaskID ();

private:
    //升级触发
    int TriggerLevelUp(int iNowLevel);

    //完成任务
    int TriggerTaskFinish(CTaskID& stTaskID);

    //宠物等级上升触发
    int TriggerPetLevelUp(int iNowPetLevel);

    //消灭BOSS
    int TriggerConquerBoss(int iBossID, int iCount);

    //消灭怪物
    int TriggerKillEnemy(int iEnemyID, int iCount);

    //进行操作
    int TriggerDoSomeOpt(int iOptID, int iCount);
    
    //使用道具
    int TriggerUseItem(unsigned char ucItemType, unsigned short usItemID, int iCount);

    //获得道具
    int TriggerGetItem(unsigned char ucItemType, unsigned short usItemID, int iCount);

    //通关
    int TriggerFinishInstance(int iInstanceID, int iCount);

    //task list update, send new status to front
    int UpdateTaskList2Front();
    //check task target status and send new status to front
    int UpdateTaskStatus();

    bool HasDoSomeOptTask();

private:
    unsigned int                                                _uiGID;                     
    bool                                                        m_bTaskTargetStatus;        //record the task status change 
    CTaskStatus _stTaskStatus;                                                              //任务状态
    CTLib::CTArray<CTaskID, MAX_OPEN_TASK>                      _astOpenTaskIDs;            //已经激活的任务
    CTLib::CTArray<CValidTask, MAX_VALID_TASK>                  _astValidTasks;             //进行中的任务
    CTLib::CTArray<CTaskWaitForOpen, MAX_WAIT_FOR_OPEN_TASK>    _astWaitForOpenTasks;       //等待激活的任务

    CTLib::CTArray<CTaskID, MAX_NEED_TO_ACCEPT>                 _astTaskNeedAccept;         //需要接受的任务ID

    CTLib::CTArray<CTaskTargetTrigger, MAX_TRIGGER_PER_TYPE>    _astLevelUpTriggers;        //等级上升触发器
    CTLib::CTArray<CTaskTargetTrigger, MAX_TRIGGER_PER_TYPE>    _astPetLevelUpTriggers;     //宠物等级上升触发器
    CTLib::CTArray<CTaskTargetTrigger, MAX_TRIGGER_PER_TYPE>    _astBossConquerTriggers;    //挑战BOSS触发器
    CTLib::CTArray<CTaskTargetTrigger, MAX_TRIGGER_PER_TYPE>    _astFinishTaskTriggers;     //任务完成触发器
    CTLib::CTArray<CTaskTargetTrigger, MAX_TRIGGER_PER_TYPE>    _astKillEnemyTriggers;      //消灭怪物触发器
    CTLib::CTArray<CTaskTargetTrigger, MAX_TRIGGER_PER_TYPE>    _astDoSomeOptTriggers;      //进行某些操作触发器
    CTLib::CTArray<CTaskTargetTrigger, MAX_TRIGGER_PER_TYPE>    _astUseItemTriggers;        //使用道具触发器
    CTLib::CTArray<CTaskTargetTrigger, MAX_TRIGGER_PER_TYPE>    _astGetItemTriggers;        //获得道具触发器
    CTLib::CTArray<CTaskTargetTrigger, MAX_TRIGGER_PER_TYPE>    _astFinishInstanceTriggers;    //通关触发器

    unsigned int _uiMarketTaskID;        //任务集市ID
};


#endif // __CUserTask_h__

